﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Libs.Content;

public partial class Pages_News_List : System.Web.UI.Page
{
    protected int cateID = 0, newsID = 0, parentCateid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        List<NewsWeb> list;
        NewsWeb _NewsWeb = new NewsWeb();

        if (!NewsUtils.CheckUrl(ref cateID, ref newsID))
        {
            Response.Redirect(Constant.HOME_PATH);
        }

        NewsCategories _Cate = new NewsCategories();
        _Cate = _Cate.Get(cateID);
        if (_Cate.FatherID != 0)
            parentCateid = _Cate.FatherID;
        else
            parentCateid = cateID;
        List<NewsCategories> lstSubCate = _Cate.GetList(parentCateid);
        if (lstSubCate.Count == 0 && parentCateid != 17 && parentCateid != 7 && parentCateid != 11 && parentCateid != 34 && parentCateid != 12 && parentCateid != 15 && parentCateid != 18)
            lstSubCate.Add(new NewsCategories() { CateID = _Cate.CateID, Name = _Cate.Name.ToUpper() });
        rptSubCate.DataSource = lstSubCate;
        rptSubCate.DataBind();
        _Cate = _Cate.Get(cateID);
        Title = _Cate.Name + " - " + Constant.SiteTitle;
        list = _NewsWeb.GetList(cateID, newsID, 1);

        if (list.Count == 0)
        {
            Response.Redirect(Constant.HOME_PATH);
        }

        _NewsWeb = list[0];
        _NewsWeb.ImageUrl = AppUtils.ImageUrl(_NewsWeb.ImageUrl, "400", "300");
        list[0] = _NewsWeb;
        rptList1.DataSource = list;
        rptList1.DataBind();

        // Tiêu điểm
        list = _NewsWeb.GetFocus(cateID, 0, 5);

        for (int i = 0; i < list.Count; i++)
        {
            NewsWeb _News = new NewsWeb();
            _News = list[i];
            _News.ImageUrl = AppUtils.ImageUrl(_News.ImageUrl, "130", "97");
            list[i] = _News;
        }

        rptFocus.DataSource = list;
        rptFocus.DataBind();

        // 2 bài tiếp theo
        list = _NewsWeb.GetList(cateID, _NewsWeb.NewsID, 2);

        if (list.Count == 0)
        {
            return;
        }

        for (int i = 0; i < list.Count; i++)
        {
            _NewsWeb = list[i];
            _NewsWeb.ImageUrl = AppUtils.ImageUrl(_NewsWeb.ImageUrl, "240", "154");
            list[i] = _NewsWeb;
        }

        rptList2.DataSource = list;
        rptList2.DataBind();

        // 10 bài tiếp theo
        list = _NewsWeb.GetList(cateID, _NewsWeb.NewsID, 10);

        if (list.Count == 0)
        {
            return;
        }

        for (int i = 0; i < list.Count; i++)
        {
            _NewsWeb = list[i];
            _NewsWeb.ImageUrl = AppUtils.ImageUrl(_NewsWeb.ImageUrl, "130", "97");
            list[i] = _NewsWeb;
        }

        rptList3.DataSource = list;
        rptList3.DataBind();

        list = _NewsWeb.GetList(cateID, _NewsWeb.NewsID, 10);

        rptOtherNews.DataSource = list;
        rptOtherNews.DataBind();

        if (list.Count == 10)
        {
            if (cateID != 0)
            {
                hlnext.HRef = NewsUtils.CateLink(cateID.ToString(), _Cate.Url, _NewsWeb.NewsID.ToString());
            }
            else
            {
                hlnext.HRef = NewsUtils.CateLink("0", _Cate.Url, _NewsWeb.NewsID.ToString());
            }
        }
        else
        {
            hlnext.Visible = false;
        }

    }
}